package com.zhongkai.service.impl;

import com.mysql.jdbc.StringUtils;
import com.zhongkai.CryptoUtil;
import com.zhongkai.StringStatic;
import com.zhongkai.app.service.IBaseDao;
import com.zhongkai.app.service.MaintainRegLogService;
import com.zhongkai.jedis.JedisClient;
import com.zhongkai.model.utils.TTHelp;
import com.zhongkai.model.utils.ZhongkaiResult;
import com.zhongkai.model3.Maintain;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 维修用户登录
 * */
@Service
@Transactional(rollbackFor = Exception.class)
public class MaintainRegLogServiceImpl implements MaintainRegLogService {

    @Autowired
    private IBaseDao iBaseDao;

    @Autowired
    private JedisClient jedisClient;
    /**
     * 维修用户登录
     * @param phone    手机号
     * @param password 密码
     * @param code     验证码
     * @param way      登录方式 1 密码登录 2验证码登录
     * */
    @Override
    public ZhongkaiResult maintainLogin(String phone, String password, String code, String way) {
        if (StringUtils.isNullOrEmpty(phone) || StringUtils.isNullOrEmpty(way)


                ) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        Maintain maintain  = (Maintain) iBaseDao.get("from Maintain ma where ma.tainPhone='"+phone+"'");

        if (maintain == null){
            return ZhongkaiResult.build(500,"用户名不存在");
        }

        if ("1".equals(way)){
            if (StringUtils.isNullOrEmpty(password)) {
                return ZhongkaiResult.build(500,"密码不能为空");
            }
            String appPassword = maintain.getTainPassword();

            String decontent = CryptoUtil.decode64(CryptoUtil.SALT, appPassword); //解密

            if (!password.equals(decontent)) {
                return ZhongkaiResult.build(500, "密码错误,登录失败");
            }



        }else if ("2".equals(way)){
            if (StringUtils.isNullOrEmpty(code)) {
                return ZhongkaiResult.build(500,"验证码不能为空");
            }

            try {
                String jedcode = jedisClient.get(phone);
                if (!code.equals(jedcode)) {
                    return ZhongkaiResult.build(400, "验证码错误");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return ZhongkaiResult.build(500, "缓存服务器连接错误");
            }
        }else {
            return ZhongkaiResult.build(500,"参数不合法");
        }

        try {
            String mainToken = maintain.getMainToken();
            if (!StringUtils.isNullOrEmpty(mainToken)){
                //   Long delete = jedisClient.delete(appUser.getToken());//生成新token之前删除及以前的
                jedisClient.expire(mainToken, 0);//过期时间为0
            }

            String generate = TTHelp.generate(); //生成Token
            maintain.setMainToken(StringStatic.TOKEN_WX + generate); //存入对象

            ObjectMapper mapper = new ObjectMapper();
            String chinasetJson = mapper.writeValueAsString(maintain); //转成json

            jedisClient.set(StringStatic.TOKEN_ME + generate, chinasetJson);  //加入缓存
            jedisClient.expire(StringStatic.TOKEN_ME + generate, 604800);//过期时间为一个星期

            iBaseDao.update(maintain);
        }catch (Exception e){
            e.printStackTrace();
            return ZhongkaiResult.build(500, "缓存服务器连接错误");
        }


        return null;
    }
}
